package sudoku.server.presen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.rmi.RemoteException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import sudoku.server.comunicaciones.Server;
import sudoku.server.persistencia.Constantes;
/**
 * @author Administrador
 */
@SuppressWarnings("serial")
public class JFServer extends JFrame {

	private javax.swing.JPanel jContentPane = null;
	private JButton jbConectar = null;
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJbConectar() {
		if (jbConectar == null) {
			jbConectar = new JButton();
			jbConectar.setBounds(8, 12, 110, 25);
			jbConectar.setText("Conectar");
			jbConectar.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					conectarODesconectar();
				}
			});
		}
		return jbConectar;
	}
     /**
     * 
     */
    protected void conectarODesconectar() {
        if (this.jbConectar.getText().equals("Conectar")) {
            try {
            	int tipoPersistencia=(this.rdbtnSistemaDeFicheros.isSelected() ? Constantes.FILE_SYSTEM : Constantes.SQLSERVER);
	            this.server.conectar(tipoPersistencia, this.btnCarpetaDeSudokus.getText());
            	this.rdbtnSistemaDeFicheros.setEnabled(false);
            	this.rdbtnSqlServer.setEnabled(false);
	            log("Conectado");
	            this.jbConectar.setText("Desconectar");
            }
            catch (Exception ex) {
                logError(ex.toString());
            }
        } else {
            try {
	            this.server.desconectar();
            	this.rdbtnSistemaDeFicheros.setEnabled(true);
            	this.rdbtnSqlServer.setEnabled(true);
	            log("Desconectado");
	            this.jbConectar.setText("Conectar");
            }
            catch (Exception ex) {
                logError("Desconectar");
            }
        }
    }
	/**
     * @param string
     */
    public synchronized void logError(String mensaje) {
        mensaje="<font color=red>" + mensaje + "</font>" + this.jepLog.getText();
        jepLog.setText(mensaje);
        jepLog.select(0, 0);
    }
    /**
     * @param string
     */
    private void log(String mensaje) {
        mensaje="<font color=blue>" + mensaje + "</font>" + this.jepLog.getText();
        jepLog.setText(mensaje);
        jepLog.select(0, 0);
    }
    /**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */    
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(7, 114, 406, 153);
			jScrollPane.setViewportView(getJepLog());
		}
		return jScrollPane;
	}
	/**
	 * This method initializes jEditorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */    
	private JEditorPane getJepLog() {
		if (jepLog == null) {
			jepLog = new JEditorPane();
			jepLog.setContentType("text/html");
		}
		return jepLog;
	}
      public static void main(String[] args) {
        JFServer j=new JFServer();
        j.setVisible(true);
    }
	/**
	 * This is the default constructor
	 */
	public JFServer() {
		super();
		initialize();
		try {
			ButtonGroup grupo=new ButtonGroup();
			grupo.add(this.rdbtnSqlServer);
			grupo.add(this.rdbtnSistemaDeFicheros);
            this.server=new Server(this);
        } catch (RemoteException e) {
            logError(e.toString());
        }		
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(419,295);
		this.setContentPane(getJContentPane());
		this.setTitle("Servidor de sudokus");
		this.addWindowListener(new java.awt.event.WindowAdapter() { 
			public void windowClosing(java.awt.event.WindowEvent e) {    
				System.exit(0);
			}
		});
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJbConectar(), null);
			jContentPane.add(getJScrollPane(), null);
			
			rdbtnSqlServer = new JRadioButton("SQL Server");
			rdbtnSqlServer.setBounds(8, 35, 110, 23);
			jContentPane.add(rdbtnSqlServer);
			
			rdbtnSistemaDeFicheros = new JRadioButton("Sistema de ficheros");
			rdbtnSistemaDeFicheros.setSelected(true);
			rdbtnSistemaDeFicheros.setBounds(130, 35, 158, 23);
			jContentPane.add(rdbtnSistemaDeFicheros);
			
			btnCarpetaDeSudokus = new JButton("/Users/EJMGN/Desktop");
			btnCarpetaDeSudokus.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ubicacionSudokus();
				}
			});
			btnCarpetaDeSudokus.setBounds(1, 70, 412, 29);
			jContentPane.add(btnCarpetaDeSudokus);
		}
		return jContentPane;
	}

    protected void ubicacionSudokus() {
		javax.swing.JFileChooser fc=new JFileChooser();
		int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.btnCarpetaDeSudokus.setText(file.getAbsolutePath());
        } 
	}

	/**
     *  
     * @uml.property name="sudokuServer"
     * @uml.associationEnd inverse="jFServer:sudoku.server.comunicaciones.SudokuServer" multiplicity="(0 1)"
     * 
     */
    private Server server;

	private JScrollPane jScrollPane = null;
	private JEditorPane jepLog = null;
	private JRadioButton rdbtnSqlServer;
	private JRadioButton rdbtnSistemaDeFicheros;
	private JButton btnCarpetaDeSudokus;
    /**
     *  
     * @uml.property name="sudokuServer"
     * 
     */
    public Server getServer() {
        return server;
    }

    /**
     *  
     * @uml.property name="sudokuServer"
     * 
     */
    public void setServer(Server server) {
        this.server = server;
    }
}
